# -*- coding: utf-8 -*-
require 'nokogiri'
require 'open-uri'

module TokuSub
  class << self
    def get(weekday)
      get_index[weekday.to_sym]
    end

    alias_method :[], :get

    %w(mon tue wed thu fri sat sun).each do |name|
      define_method(name) { get(name) }
    end

    def get_index
      # get tokusub page
      tokusub_page = Nokogiri::HTML(open('http://www.subway.co.jp/campaign/tokusub/'))
      h = Hash::new

      # index tokusubs
      tokusub_page.css('li a img').each do |img|
        if img['src'] =~ /^images\/top_menu0/ then
          case img['src']
          when 'images/top_menu01.jpg'
            h.store(:mon, img['alt'])
          when 'images/top_menu02.jpg'
            h.store(:tue, img['alt'])
          when 'images/top_menu03.jpg'
            h.store(:wed, img['alt'])
          when 'images/top_menu04.jpg'
            h.store(:thu, img['alt'])
          when 'images/top_menu05.jpg'
            h.store(:fri, img['alt'])
          when 'images/top_menu06.jpg'
            h.store(:sat, img['alt'])
          when 'images/top_menu07.jpg'
            h.store(:sun, img['alt'])
          end
        end
      end

      h
    end
  end
end
